﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Sanchay
{
    public partial class StockMovement : Form
    {
        string _partsNo="";
        string _partsName = "";
        public StockMovement()
        {
            InitializeComponent();
        }

        public StockMovement(string partsNo, string partsName)
        {
            _partsNo = partsNo;
            _partsName = partsName;
            InitializeComponent();
        }

        public void StockMovement_Load(object sender, EventArgs e)
        {
            dtpFrom.Value = Convert.ToDateTime("01-Apr-" + (DateTime.Today.Year));
            dtpTo.Value = Convert.ToDateTime("31-Mar-" + (DateTime.Today.Year + 1));

            SearchStockMovement();

            this.Text = " Stock Movement - " + _partsName + "(" + _partsNo + ")";

        }

        private void dtpFrom_ValueChanged(object sender, EventArgs e)
        {
            SearchStockMovement();
        }

        private void SearchStockMovement()
        {
            DataAccess da = new DataAccess();
            string query = "SELECT TransactionType.TransactionType AS [Transaction Type], InventoryMaster.InvoiceNo AS [Invoice No.], " +
                      "InventoryMaster.TransactionDate AS Date, InventoryMaster.Qty, InventoryMaster.UnitPrice AS [Unit Price], " +
                      "InventoryMaster.TotalPrice AS [Total Price] " +
                      "FROM InventoryMaster INNER JOIN " +
                      "TransactionType ON InventoryMaster.TransactionType = TransactionType.TransactionTypeId " +
                      "WHERE InventoryMaster.PartsNo = '" + _partsNo + "' " +
                      "AND TransactionDate BETWEEN '" + dtpFrom.Value.ToString("MM/dd/yyyy") + "' AND '" + dtpTo.Value.ToString("MM/dd/yyyy") + "' " +
                      "ORDER BY TransactionDate desc";

            DataTable dt = da.SelectCommand(query);
            dgvStockMovement.DataSource = dt.DefaultView;

            dgvStockMovement.Columns[0].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleLeft;
            dgvStockMovement.Columns[1].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleLeft;
            dgvStockMovement.Columns[2].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleLeft;
            dgvStockMovement.Columns[3].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
            dgvStockMovement.Columns[4].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight;
            dgvStockMovement.Columns[5].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight;


            dgvStockMovement.Columns[3].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            dgvStockMovement.Columns[4].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
            dgvStockMovement.Columns[5].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
        }

        private void dtpTo_ValueChanged(object sender, EventArgs e)
        {
            SearchStockMovement();
        }
    }
}
